﻿namespace HAOest.Text
{
    /// <summary>
    /// 文本替换类，提供多种替换文本的方法
    /// <para>作者：邓斌HADB</para>
    /// <para>修订：2012-09-28 V1.0</para>
    /// </summary>
    public class ReplaceText
    {
        /// <summary>
        /// 简单替换，和string的Replace方法一样
        /// </summary>
        /// <param name="allText">原文本</param>
        /// <param name="oldValue">要替换的字符串</param>
        /// <param name="newValue">新字符串</param>
        /// <returns>替换后的文本</returns>
        public static string SimpleReplace(string allText, string oldValue, string newValue)
        {
            return allText.Replace(oldValue, newValue);
        }

        /// <summary>
        /// 替换定位字符串后面的N位字符串
        /// <para>例子：要将"search=old"中的"old"替换为"new"，则</para>
        /// <para>　　　allText = "search=old"</para>
        /// <para>　　　searchstring = "search="</para>
        /// <para>　　　replaceLength = 3</para>
        /// <para>　　　newstring = "new"</para>
        /// </summary>
        /// <param name="allText">原文本</param>
        /// <param name="searchstring">定位字符串</param>
        /// <param name="replaceLength">定位字符串后要替换的位数</param>
        /// <param name="newstring">新字符串</param>
        /// <returns>替换之后的文本</returns>
        public static string ReplaceAfterSearchstring(string allText, string searchstring, int replaceLength, string newstring)
        {
            int searchIndex = allText.IndexOf(searchstring);
            string oldValue = searchstring + allText.Substring(searchIndex + searchstring.Length, replaceLength);
            string newValue = searchstring + newstring;
            allText = allText.Replace(oldValue, newValue);
            return allText;
        }
    }
}
